package com.idealighter.game.games.errenniuniu;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.dictionary.domain.ErrenniuniuRoomDomain;
import com.idealighter.game.dictionary.domain.RobotConfigDomain;
import com.idealighter.game.gamehall.dto.SeatInfo;
import com.idealighter.game.games.errenniuniu.message.ReqEnterGameHallMsg;
import com.idealighter.game.games.errenniuniu.message.ReqEnterRoomMsg;
import com.idealighter.game.games.errenniuniu.message.ReqEnterTableMsg;
import com.idealighter.game.robot.core.Player;

/**
 * 二人牛牛机器人.
 * 
 * @date 2016年3月23日 下午8:09:59
 *
 */
public class ErRenNiuNiuPlayer extends Player {

  public final ErrenniuniuRoomDomain roomDomain;

  /**
   * 构造函数.
   * 
   * @param playerId . .
   * @param userName . .
   * @param roomId . .
   * @param initGold . .
   * @param totalGameNum . .
   * @param tableGameNum . .
   */
  public ErRenNiuNiuPlayer(long playerId, String userName, int roomId, int initGold,
      int totalGameNum, int tableGameNum, RobotConfigDomain configDomain,
      ErrenniuniuRoomDomain errenniuniuRoomDomain) {
    super(playerId, userName, roomId, initGold, totalGameNum, tableGameNum, configDomain);
    this.roomDomain = errenniuniuRoomDomain;
  }

  @Override
  public boolean goldCheck() {

    long gold = playerInfo.getGold();
    if (gold < roomDomain.getLower()) {
      LOG.info("[二人牛牛]机器人[{}][{}]金币小于房间下限退出游戏", userName, playerId);
      delayLogout();
      return false;
    }

    if (gold > roomDomain.getUpper()) {
      LOG.info("[二人牛牛]机器人[{}][{}]金币高于房间下限退出游戏", userName, playerId);
      delayLogout();
      return false;
    }

    if (gold < roomDomain.getAfee()) {
      LOG.info("[二人牛牛]机器人[{}][{}]金币小于台费退出游戏", userName, playerId);
      delayLogout();
      return false;
    }
    return true;
  }

  @Override
  public void enterGameHall() {
    sendMsg(new ReqEnterGameHallMsg());
  }

  @Override
  public void enterRoom() {
    if (rooms.contains(roomId)) {
      if (!goldCheck()) {
        return;
      }
      ReqEnterRoomMsg msg = new ReqEnterRoomMsg();
      msg.setRoomId(roomId);
      sendMsg(msg);
    }
  }

  @Override
  public void enterTable() {
    if (!goldCheck()) {
      return;
    }
    SeatInfo emptySeat = findEmptySeat();
    if (emptySeat != null) {
      ReqEnterTableMsg msg = new ReqEnterTableMsg();
      msg.setTableId((int) emptySeat.getTableId());
      msg.setOrder((byte) emptySeat.getOrder());
      sendMsg(msg);
    }
  }

  @Override
  public Game game() {
    return Game.ERREN_NIUNIU;
  }

  @Override
  public boolean enterPwdTableAble() {
    return false;
  }

}
